/**
 * Created by .
 * User: SiAVoL
 * Date: 19.05.11
 * Time: 23:33
 * To change this template use File | Settings | File Templates.
 */
function init() {
    var canvas = document.getElementById("gameCanvas");
    canvas.width = 640;
    canvas.height = 640;
    var context = canvas.getContext("2d");

    var field = new game();
    field.setFieldSize(3);
    field.createField();
    
    var cellSize = canvas.width / field.getFieldSize();
    field.setCellView(function(x, y) {
        context.fillStyle = "#FFB93B";
        context.fillRect(x+1, y+1, cellSize-2, cellSize-2);
    });
    field.setCrossView(function(){
        context.font = "bold "+(cellSize/2)+"px Sans";
        context.textAlign = "center";
        context.textBaseline = "middle";
        context.fillStyle = "#222";
    })
    field.setZeroView(function(){
        context.font = "bold "+(cellSize/2)+"px Sans";
        context.textAlign = "center";
        context.textBaseline = "middle";
        context.fillStyle = "#222";
    })

    context.fillStyle = "#222";
    context.fillRect(0, 0, canvas.width, canvas.height);
    field.draw(context, cellSize);

    function event(x, y) {
        field.setCell(y, x, field.cellContent.zero);
        context.fillStyle = "#222";
        context.fillRect(0, 0, canvas.width, canvas.height);
        field.draw(context, cellSize);
    }

    canvas.onclick = function(e) {
        var x = (e.pageX - canvas.offsetLeft) / cellSize | 0;
        var y = (e.pageY - canvas.offsetTop) / cellSize | 0;
        event(x, y);
    }
}

function game() {
    var cellView = null;
    var crossView = null;
    var zeroView = null;
    var fieldSize = 19;
    var field = [];

    this.cellContent = {"none":0, "zero":1, "cross":2}

    // внешний вид клетки
    this.setCellView = function(func) {
        cellView = func;
    }

    // внешний вид крестика
    this.setCrossView = function(func) {
        crossView = func;
    }

    // внешний вид нолика
    this.setZeroView = function(func) {
        zeroView = func;
    }

    this.getFieldSize = function() {
        return fieldSize;
    }
    this.setFieldSize = function(value) {
        fieldSize = value;
        this.createField();
    }

    this.createField = function() {
        field = [];
        for (var i = 0; i < fieldSize; i++) {
            field[i] = [];
            for (var j = 0; j < fieldSize; j++)
            {
                field[i][j] = this.cellContent.none;
            }
        }
    }

    this.getCell = function(col, row) {
        return field[col][row];
    }
    this.setCell = function(col, row, value) {
        field[col][row] = value;
    }

    // метод отрисовки
    this.draw = function(context, size) {
        for (var i = 0; i < fieldSize; i++)
            for (var j = 0; j < fieldSize; j++){
                if (cellView != null) {
                    cellView(j * size, i * size);
                }
                if (field[i][j] == this.cellContent.cross){
                    crossView();
                    context.fillText("X", j*size + size/2, i*size + size/2);
                }
                if (field[i][j] == this.cellContent.zero){
                    zeroView();
                    context.fillText("O", j*size + size/2, i*size + size/2);
                }
            }
    }
}